package de.tum.wi.som.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import de.tum.wi.som.Project;

public class RCPFileUtil {

	protected static String DATA_PATH = "data";

	public static Project loadFile(String fileName) {

		Path p = Paths.get(System.getProperty("user.dir"), DATA_PATH, fileName);

		InputStream in = null;
		Project project = null;

		try {
			in = new FileInputStream(p.toString());
			project = Project.readProject(in, p.getFileName().toString());
		} catch (FileNotFoundException e) {
			System.err.println("File not found at '" + p.toString() + "'.");
		} catch (IOException e2) {
			System.err.println("File at '" + p.toString()
					+ "' couldn't be handled.");
		}

		return project;
	}
	
	public static List<Project> loadAllFiles(String directory) {

		List<Project> projects = new ArrayList<Project>();
 		Path p = Paths.get(System.getProperty("user.dir"), DATA_PATH, directory);

		File dir = p.toFile();
		
		InputStream in = null;
		Project project = null;

		
		for (File f : dir.listFiles()){
			if (f.isDirectory())
				continue;
			
			try {
				in = new FileInputStream(f);
				project = Project.readProject(in, f.getName());
			} catch (FileNotFoundException e) {
				System.err.println("File not found at '" + f.getName() + "'.");
			} catch (IOException e2) {
				System.err.println("File at '" + p.toString()
						+ "' couldn't be handled.");
			}
			projects.add(project);
		}

		

		return projects;
	}

}
